package com.mondo.financialwallet.presentation.ui.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.common.ui.AbsNetworkStateFragment;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.ProductListEntity;
import com.mondo.financialwallet.presentation.model.HotProducts;
import com.mondo.financialwallet.presentation.ui.activity.ProductDetailsActivity;
import com.mondo.financialwallet.presentation.ui.adapter.HotProductsAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 理财产品
 */
public class FinancialProductFragment extends AbsNetworkStateFragment implements
        BaseQuickAdapter.OnItemClickListener {

    @Bind(R.id.hot_product_list) RecyclerView mHotProductList;
    @Bind(R.id.name_text_view) TextView mNameTextView;
    @Bind(R.id.type_text_view) TextView mTypeTextView;
    @Bind(R.id.annualized_rate_text) TextView mAnnualizedRateText;
    @Bind(R.id.minimum_day_text) TextView mMinimumDayText;
    @Bind(R.id.minimum_amount_text) TextView mMinimumAmountText;
    @Bind(R.id.investment_now) View mInvestmentView;
    @Bind(R.id.product_layout) View mProductLayout;

    private HotProductsAdapter mAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_financial_product;
    }

    @Override
    protected void initActions() {
        // 理财产品列表
        mAdapter = new HotProductsAdapter();
        mAdapter.setOnItemClickListener(this);
        mHotProductList.setAdapter(mAdapter);

        // 请求理财产品列表
        requestHotFinancialProductList();
    }

    private void requestHotFinancialProductList() {
        loadingNetwork();

        DataRepository.get().productList(result -> {
            if (result.isSuccess()) {
                successNetwork();

                List<ProductListEntity.DataBean> products = result.getData();
                List<HotProducts> productsModel = new ArrayList<>();
                for (ProductListEntity.DataBean product : products) {
                    productsModel.add(new HotProducts(
                            product.getGoods_type(),
                            product.getId(),
                            product.getName(),
                            product.getYear_profit_rate(),
                            product.getLimit_money(),
                            String.valueOf(product.getEntrust_period())
                    ));
                }

                displayProductInView(productsModel);
            } else {
                errorNetwork(result.getMsg());
            }
        });
    }

    private void displayProductInView(List<HotProducts> products) {
        HotProducts hotProducts = products.get(0);
        mNameTextView.setText(hotProducts.getTitle());
        mAnnualizedRateText.setText(hotProducts.getAnnualizedRate());
        mMinimumAmountText.setText(hotProducts.getMinimumAmount());
        mMinimumDayText.setText(String.format(getString(R.string.text_placeholder_day),
                hotProducts.getMinimumDay()));
        mInvestmentView.setOnClickListener(v ->
                ProductDetailsActivity.launch(v.getContext(), hotProducts));

        // 产品列表
        mAdapter.setNewData(products.subList(1, products.size()));
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HotProducts product = mAdapter.getItem(position);
        ProductDetailsActivity.launch(view.getContext(), product);
    }

    @Override
    public void onAnewRequest() {
        requestHotFinancialProductList();
    }
}
